package site.web;

import java.sql.SQLException;
import java.util.Date;

import neoutil.CommonAction;
import neoutil.Page;

import org.apache.log4j.Logger;

import site.entity.Resource;
import site.entity.Review;
import site.entity.User;

/**
 * @type ReviewAction
 * @info author:陈亚鹏 date:2012-9-21 下午07:31:39 version:1.0
 */
public class ReviewAction extends CommonAction {

	private static final long serialVersionUID = -6585657913445656302L;
	private static Logger log = Logger.getLogger(ReviewAction.class);

	private Review review;
	private Resource resource;

	// 通过-2 回退-1 拒绝-0
	private int status;
	private final int PASS = 2;
	private final int UNPASS = 1;
	private final int REFUSE = 0;

	private String advice;

	private Page<Review> page = new neoutil.Page<Review>(15);
	
	/**
	 * 添加一条审核记录
	 * @method addReview
	 * @info author:陈亚鹏  date:2012-9-22 下午06:11:09 version:1.0
	 * @return String
	 */
	public String addReview() {
		try {
			User user = (User) getSessionObject("loginUser");
			if(user==null)return RELOGIN;
			review = new Review();
			review.setResourceid((int) resource.getId());
			review.setReviewerid((int) user.getId());
			review.setReviewername(user.getName());
			if(advice.trim().equals("")) advice="(空)";
			review.setAdvice(advice);
			review.setStatus(status);
			getReviewRepository().insert(review);

			resource = getResourceRepository().getResourceByUuid(resource.getUuid());
			
			if (status == PASS && user.getRid() <= 2) {
				resource.setStatus(3 - user.getRid());
				
				if(resource.getStatus()==Resource.STATUS_THREEPASS)
				{
					getResourceRepository().SynsResource(resource,"insert");
				}
			} else if (status == REFUSE) {
				resource.setStatus(Resource.STATUS_REFUSE);
			} else if (status == UNPASS) {
				resource.setStatus(Resource.STATUS_UNPASS);
			}

			getResourceRepository().updateResource(resource);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	/**
	 * 删除指定的审核记录
	 * @method deleteReview
	 * @info author:陈亚鹏  date:2012-9-22 下午06:11:25 version:1.0
	 * @return String
	 */
	public String deleteReview()
	{
		String id = getRequestParameter("id");
		try {
			review = getReviewRepository().findById(Integer.parseInt(id));
			resource = getResourceRepository().findById(review.getResourceid());
			getReviewRepository().delete(review);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String reviewResourceByUuid() {
		try {
			User user = (User) getSessionObject("loginUser");
			String uuid = getRequestParameter("uuid");
			resource = getResourceRepository().getResourceByUuid(uuid);
			if (user == null) {
				log.error("用户未登录！");
				return RELOGIN;
			} else {
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	/**
	 * 由资源的Uuid查找审核历史,若未登陆跳转到登陆
	 * @method getReviewHistoryByUuid
	 * @info author:陈亚鹏  date:2012-9-22 下午06:11:48 version:1.0
	 * @return String
	 */
	public String getReviewHistoryByUuid() {
		try {
			String uuid = getRequestParameter("uuid");
			resource = getResourceRepository().getResourceByUuid(uuid);

			User loginUser = (User) getSessionObject("loginUser");
			if (loginUser.getRid() <= 2) {
				page = getReviewRepository().getReviewsByRid(page, (int) resource.getId());
			}
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public void setResource(Resource resource) {
		this.resource = resource;
	}

	public Resource getResource() {
		return resource;
	}

	public void setReview(Review review) {
		this.review = review;
	}

	public Review getReview() {
		return review;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public int getStatus() {
		return status;
	}

	public void setAdvice(String advice) {
		this.advice = advice;
	}

	public String getAdvice() {
		return advice;
	}


	public Page<Review> getPage() {
		return page;
	}

	public void setPage(Page<Review> page) {
		this.page = page;
	}

}
